<?php namespace Common\Model;
use Hdphp\Model\Model;
class Link extends Model{
	//数据表
	protected $table = "link";
	 
	//自动验证，必须通过create方法来触发
//	| lid | lname | addtime | logo | url | sort
	protected $validate = array( 
	array("lname","required","链接名称不能为空",3,3),
	array("url","required","链接名称不能为空",3,3),
	array("url","http","网址不正确",3,3),
	array("sort","num:0,199743","排序必须为数字",3,3),
	);
	//自动完成
	protected $auto = array(
	  array("addtime","time","function",3,1),
	  //自定义一个自动完成方法
	   array("logo","image","method",3,3),
	);
	/*
	 * 自定义方法IMAGE图片上传
	 * */
 public function image(){
 	//这个为编辑链接写的
	 //如果隐藏域里面的thumb,直接把隐藏域里面的路径返出
   	if(isset($_POST['logo'])){
   		return $_POST['logo'];
   	}
	$path = "Upload/Logo/".date("ymd");
	$file = Upload::type("jpg,png")->path($path)->size(20000)->make();
	//如果有文件上传
	if($file){
		$newpath = Image::thumb($file[0]['path'],$path."/thumb".$file[0]['basename'],200,200,5);
	   return $newpath;
	   }else{
	   return "";
	   }
	}
	/*
	 * 添加
	 * */
	public function store(){
		//自动验证
	 	if(!$this->create()) return false;
		//上传错误判断
		if($_FILES['logo']['error']!=4){
		//上传错误信息
			if($error = Upload::getError()){
				$this->error = $error;
				return false;
			}
	    }
		$this->add();
        return true;
	}
	public function edit(){
		if(!$this->create()) return false;
		if(isset($_FILES['logo']) && $_FILES['logo']['error']!=4)
		{
			//上传错误信息
			if($error = Upload::getError()){
				$this->error = $error;
				return false;
			}
		}
		$this->save();
        return true;}
	
	
}
